package com.sinosoft.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.sinosoft.base.BaseFormController;
import com.sinosoft.base.Constants;
import com.sinosoft.product.ProductDisplay;
import com.sinosoft.product.ProductDisplayManager;
import com.sinosoft.shopcar.ShopcarManager;
import com.sinosoft.util.DisplayGetPage;
import com.sinosoft.util.SystemConfigUtil;
import com.sinosoft.util.SystemLogUtil;

@Controller
public class ProductDisplayController extends BaseFormController {

	private ProductDisplayManager mgr = null;

	@Autowired
	public void setMgr(ProductDisplayManager mgr) {
		this.mgr = mgr;
	}

	private MessageSource messageSource = null;

	@Autowired
	public void setMessageSource(MessageSource messageSource) {
		this.messageSource = messageSource;
	}

	private ShopcarManager shopcar = null;

	@Autowired
	public void setShopcar(ShopcarManager shopcar) {
		this.shopcar = shopcar;
	}

	// the method for product display
	@SuppressWarnings("unchecked")
	@RequestMapping("/util/productDisplay.html")
	public ModelAndView display(HttpServletRequest request, Model model) {

		int pageEvery = 12;// 每页显示的条数
		if (null != request.getParameter("pageNumber")) {
			pageEvery = Integer.parseInt(request.getParameter("pageNumber"));
		}

		// int row = 3;// 每页显示图片的列数
		// if (null != request.getParameter("row")) {
		// row = Integer.parseInt(request.getParameter("row"));
		// }
		//
		// int areaLong = 300;// 图片的长宽
		// if (null != request.getParameter("areaLong")) {
		// areaLong = Integer.parseInt(request.getParameter("areaLong"));
		// }

		MessageSourceAccessor text = new MessageSourceAccessor(messageSource,
				request.getLocale());
		String strsql = SystemConfigUtil.getSystemSql("productSQL",
				getServletContext());// 取数据字典
		String page = DisplayGetPage
				.getPageParamName("productDisplay", request);// 第几页

		ArrayList<ProductDisplay> alist = new ArrayList();
		HashMap displaystr = new HashMap();

		int count = mgr.getCount(strsql);
		List list = mgr.getProduct(strsql, (Integer.parseInt(page) - 1)
				* pageEvery + 1, Integer.parseInt(page) * pageEvery, request,
				messageSource);
		alist = (ArrayList<ProductDisplay>) list;
		for (int i = 0; i < alist.size(); i++) {
			ProductDisplay product = new ProductDisplay();
			product = alist.get(i);

			String str0 = " ";
			String str1 = " ";
			String str2 = " ";
			String str3 = " ";
			String str4 = " ";
			String str5 = " ";
			String date = " ";
			if (null != product.getProductid()) {
				str0 = product.getProductid();
			}
			if (null != product.getSatelliate()) {
				str1 = product.getSatelliate();
			}
			if (null != product.getSonser()) {
				str2 = product.getSonser();
			}
			if (null != product.getScenedate()) {
				str3 = product.getScenedate();
				date = str3.substring(0, 9);
			}
			if (null != product.getBrowsefile()) {
				str4 = product.getBrowsefile();
				String[] imgstr = str4.split(",");
				if(imgstr.length>1)
					str4 = imgstr[1];
			}
			if (null != product.getProductlevel()) {
				str5 = product.getProductlevel();
			}

			String oneProduct = str0 + "_" + str5 + "_" + str1 + "_" + str2;

			// product.setSatelliate(str1);
			// product.setSonser(str2);
			// product.setScenedate(str3);
			// product.setBrowsefile(str4);
			//
			// product.setDisplaystr(text.getMessage("collect.satelliteid") +
			// ":"
			// + str1 + text.getMessage("collect.sensorid") + ":" + str2
			// + text.getMessage("product.collectdate") + ":" + str3);
			// alist.add(product);
			Subject subject = SecurityUtils.getSubject();
			String state = " ";
			String username = "";
			String str = "";
			if (null != subject.getPrincipal()) {
				username = subject.getPrincipal().toString();
				str = str0 + "_" + str5 + "_" + username;
				int param = mgr.getShopCarStatus(str);
				if (param == 1) {
					state = "yes";
				} else {
					state = "no";
				}
			} else {
				state = "read";
			}

			// displaystr.put(pro[0].toString(), pro[4].toString() + "@@@"
			// + text.getMessage("collect.satelliteid") + ":"
			// + pro[1].toString() + text.getMessage("collect.sensorid")
			// + ":" + pro[2].toString()
			// + text.getMessage("product.collectdate") + ":"
			// + pro[3].toString() + "@@@" + state);
			displaystr.put(str0, str4 + "###" + oneProduct + "@@@" + str1
					+ "@@@" + str2 + "@@@" + date + "@@@" + state);
		}
		request.setAttribute("resultSize", count);
		request.setAttribute("display", displaystr);

		double pageEve = pageEvery;
		model.addAttribute("displayallpage", Math.ceil(count / pageEve));
		model.addAttribute("thispage", Integer.parseInt(page));

		request.setAttribute("pageSize", pageEvery);
		// request.setAttribute("row", row);
		// request.setAttribute("areaLong", areaLong);
		
		return new ModelAndView("util/productDisplay", "prolist", alist);

	}

	// the method for product add to the shop car
	@RequestMapping("/shopcar/productDisplay.html")
	public String addtoshopcar(ProductDisplay productDis,
			HttpServletRequest request) {

		MessageSourceAccessor text = new MessageSourceAccessor(messageSource,
				request.getLocale());
		int pageEvery = Integer.parseInt(request.getParameter("pageNum"));
		// int row = Integer.parseInt(request.getParameter("rowNum"));
		// int areaLong = Integer.parseInt(request.getParameter("longNum"));

		String strsql = SystemConfigUtil.getSystemSql("productSQL",
				getServletContext());
		if (null != productDis.getProductid()) {
			String[] productid = productDis.getProductid().split(",");
			for (int i = 0; i < productid.length; i++) {
				String str = mgr.getProductStr(strsql + " and t.productid = "
						+ "'" + productid[i] + "'", request, messageSource);
				// str = str.toUpperCase();
				String[] levelkey = str.split(",");

				/**
				 * 添加产品到购物车
				 * 
				 * @param level
				 *            产品级别
				 * @param keys
				 *            ([satellite_sensorid_productid,
				 *            satellite1_sensorid1_productid1])
				 * @return
				 */
				try {
					shopcar.saveShopcarListByProductInfo(levelkey[0],
							levelkey[1]);
				} catch (Exception e) {
					SystemLogUtil.saveError(Constants.LOG_ERROR,
							"pruductDisplay to shopcar fail!");
					this.saveError(request, text
							.getMessage("addToShopCar.fail"));
				}

			}

			this.saveMessage(request, text.getMessage("addToShopCar.success"));
		} else {
			this.saveError(request, text.getMessage("addToShopCar.fail"));
		}

		String pagename = DisplayGetPage.getPageName("productDisplay", request);
		String page = request.getParameter("tz");

		return "redirect:/util/productDisplay.html?pageNumber=" + pageEvery
				+ "&" + pagename + "=" + page;

		// return "redirect:/util/productDisplay.html?method=list&pageNumber="
		// + pageEvery + "&row=" + row + "&areaLong=" + areaLong;

	}

	// 点击产品跳转到产品详情
	@RequestMapping("/toProductPage/oneProductDisplay.html")
	public String toProductPage(HttpServletRequest request) {

		String page = request.getParameter("onePage");// 5717_LEVEL2C_ZY02C_PMS
		String[] str = page.split("_");
		
		return "redirect:/scene/sceneview.html?method=view&sceneid=&productid="
				+ str[0] + "&productlevel=" + str[1] + "&satelliteid=" + str[2]
				+ "&sensorid=" + str[3] + "";
	}

}
